package by.grsu.verkhovodko.page;

import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.verkhovodko.app.VerkhovodkoWebApplication;
import by.grsu.verkhovodko.dao.impl.TrainDao;
import by.grsu.verkhovodko.entity.Train;


public class TrainEditPage extends WebPage {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6103341272078555117L;

	
	public TrainEditPage()
	{
		this(new Train());
	}
	
	@SuppressWarnings({ "serial", "rawtypes" })
	public TrainEditPage(final Train train) {
		super();
		

		final Form<Train> form = new Form<Train>("form", new CompoundPropertyModel<Train>(train));
		add(form);

		form.add(new TextField<String>("destStation"));	
		form.add(new TextField<Integer>("cost"));		
		form.add(new DateTextField("dateOfDeparture", "DD.mm.yy HH:MM"));
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				TrainDao trainDao = new TrainDao(VerkhovodkoWebApplication.DATA_XML_FOLDER);
				if (train.getId() == null) {
					System.out.println("save="+train);
					trainDao.saveNew(train);
				} else {
					System.out.println("update="+train);
					trainDao.update(train);
				}

				setResponsePage(new TrainListPage());
			}
		});
		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new TrainListPage());
			}
		});
		
	}
}
